import 'package:flutter/material.dart';
import 'package:flutter_app/module/home/marker/view.dart';
import 'package:flutter_app/module/home/optional/view.dart';
import 'package:get/get.dart';

import 'compose/view.dart';
import 'fund/view.dart';
import 'logic.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  final HomeLogic logic = Get.put(HomeLogic());

  List tabs = ["自选", "市场", "基金", "组合"];
  late TabController _tabController;

  late PageController _pageController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
    _pageController = PageController();
    _tabController.addListener(() {
      setState(() {
        _pageController.jumpToPage(_tabController.index);
      });
    });


  }

  @override
  void dispose() {
    super.dispose();
    _tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        top: true,
        bottom: true,
        child: Column(
          children: [
            _buildAppBar(),
            Expanded(
                flex: 1,
                child: PageView(
                  physics: NeverScrollableScrollPhysics(),
                  controller: _pageController,
                  children: [
                    OptionalPage(),
                    MarkerPage(),
                    FundPage(),
                    ComposePage(),
                  ],
                ))
          ],
        ),
      ),
    );
  }

  _buildAppBar() {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 14.0),
          child: ClipOval(child: Icon(Icons.home)),
        ),
        Expanded(child: _buildTabs(), flex: 1),
        Padding(
          padding: const EdgeInsets.only(left: 14.0),
          child: Icon(Icons.search),
        ),
        Padding(
          padding: const EdgeInsets.symmetric(horizontal: 14.0),
          child: Icon(Icons.message),
        ),
      ],
    );
  }

  _buildTabs() {
    return TabBar(
        physics: null,
        indicatorColor: Colors.transparent,
        isScrollable: true,
        controller: _tabController,
        tabs: tabs.map((e) {
          return Tab(text: e);
        }).toList());
  }
}
